package it.controllocredito.frontend.fragments;

import java.util.LinkedList;
import java.util.List;

import it.controllocredito.R;
import it.controllocredito.backend.persistence.CalllogPersistenceManager;
import it.controllocredito.backend.persistence.bean.RegistroChiamate;
import it.controllocredito.backend.utility.Utilities;
import it.controllocredito.frontend.util.RegistroChiamateArrayAdapter;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import android.view.ActionMode;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Menu;

public class CallTabFragment extends SherlockFragment {

	private static final String	log_class_name	= "CallTabFragment";
	private ActionMode mActionMode;
	public int pos;

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
		return inflater.inflate(R.layout.call_tab_fragment, group, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.v(log_class_name, "onActivityCreated");
		
		CalllogPersistenceManager calllogPersistenceManager = new CalllogPersistenceManager(this.getActivity().getApplicationContext());
		
		Log.d(log_class_name, "Getting RegistroChiamate list");
		
		final List<RegistroChiamate> listaRegistroChiamate = calllogPersistenceManager.getRecordsRegistroChiamate();
		final RegistroChiamateArrayAdapter registroChiamateArrayAdapter = new RegistroChiamateArrayAdapter(this.getActivity().getApplicationContext(), listaRegistroChiamate);

		final ListView listView = (ListView) getView().findViewById(R.id.mylist);
		Log.d(log_class_name, "Populating ListView");
		listView.setAdapter(registroChiamateArrayAdapter);
		
		Log.d(log_class_name, "Calculationg total for Outgoing calls");
		final TextView textLabelTot = (TextView) getView().findViewById(R.id.tot_uscita);
		textLabelTot.setText(Utilities.calculateTotal(listaRegistroChiamate, CallLog.Calls.OUTGOING_TYPE));
		
//		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
//			        final RegistroChiamate item = (RegistroChiamate) parent.getItemAtPosition(position);
//			            	listaRegistroChiamate.remove(item);
//			        		textLabelTot.setText(Utilities.calculateTotal(listaRegistroChiamate, CallLog.Calls.OUTGOING_TYPE));
//			            	registroChiamateArrayAdapter.notifyDataSetChanged();
//			            	
//			      }
//		});
		
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
		listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
			int index;
//			List<Integer> ids = new LinkedList<Integer>();

			 @Override
			public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
				this.index = position;
				//Ogni volta che cambia lo stato di un item sulla lista (checked/unchecked)
//	            Log.i(log_class_name, "onItemCheckedStateChanged: "+index);
//	            if(checked){
//	            	Log.i(log_class_name, "checked");
//	            	ids.add(Integer.valueOf(index));
//	            }
//	            else{
//	            	Log.i(log_class_name, "unchecked");
//	            	ids.remove(Integer.valueOf(index));
//	            }
			}
			
		    @Override
		    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//		    	for(Integer i: ids){
//		    		Log.i(log_class_name, "check:"+i );
//		    	}
		    	//Risponde agli eventi sul CAB
		    	Log.i(log_class_name, "onActionItemClicked: "+item.getItemId());
		        switch (item.getItemId()) {
		            case R.id.menu_delete:
		            	Log.i(log_class_name, "Removing item at position: "+index);
		            	RegistroChiamateArrayAdapter adapter = (RegistroChiamateArrayAdapter) listView.getAdapter(); 
		            	List<RegistroChiamate> lista = adapter.getValues();
		            	lista.remove(listView.getItemAtPosition(index));
		            	registroChiamateArrayAdapter.notifyDataSetChanged();
		                mode.finish(); // Action picked, so close the CAB
		                return true;
		            default:
		                return false;
		        }
		    }

		    @Override
		    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
		        // Inflate per il CAB
		        MenuInflater inflater = mode.getMenuInflater();
		        inflater.inflate(R.menu.contextual_action_bar, menu);
		        return true;
		    }

		    @Override
		    public void onDestroyActionMode(ActionMode mode) {
		        // Here you can make any necessary updates to the activity when the CAB is removed. By default, selected items are deselected/unchecked.
		    }

		    @Override
		    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
		        // Here you can perform updates to the CAB due to an invalidate() request
		        return false;
		    }
		});
		
//		final ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
//
//		    // Called when the action mode is created; startActionMode() was called
//		    @Override
//		    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//		        // Inflate a menu resource providing context menu items
//		        MenuInflater inflater = mode.getMenuInflater();
//		        inflater.inflate(R.menu.contextual_action_bar, menu);
//		        return true;
//		    }
//
//		    // Called each time the action mode is shown. Always called after onCreateActionMode, but
//		    // may be called multiple times if the mode is invalidated.
//		    @Override
//		    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
//		        return false; // Return false if nothing is done
//		    }
//
//		    // Called when the user selects a contextual menu item
//		    @Override
//		    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//		        switch (item.getItemId()) {
//		            case R.id.menu_delete:
////		                deleteCurrentItem();
//		                mode.finish(); // Action picked, so close the CAB
//		                return true;
//		            default:
//		                return false;
//		        }
//		    }
//
//		    // Called when the user exits the action mode
//		    @Override
//		    public void onDestroyActionMode(ActionMode mode) {
//		        mActionMode = null;
//		    }
//		};
		
//		listView.setOnLongClickListener(new View.OnLongClickListener() {
//		    // Called when the user long-clicks on someView
//		    public boolean onLongClick(View view) {
//		        if (mActionMode != null) {
//		            return false;
//		        }
//
//		        // Start the CAB using the ActionMode.Callback defined above
//		        mActionMode = getActivity().startActionMode(mActionModeCallback);
//		        view.setSelected(true);
//		        return true;
//		    }
//		});
		
	}
	
	
}
